/*
* © 2010 Autodata Solutions Company. All Rights Reserved
* This source code is the confidential and proprietary information of Autodata Solutions Company.
* The user shall not, in whole or in part, copy, publish, disclose or make any use of this source code
* without the prior written consent of Autodata Solutions Company.
*/
Ext.ns('net.autodata.compare.data.photos');
(function () {
	
	/**
	 * @class PhotosProxy
	 * @extends net.autodata.data.HttpCacheProxy
	 * @namespace net.autodata.compare.data.photos
	 */
	var Proxy = Ext.extend(net.autodata.data.HttpCacheProxy, {
		
		constructor: function ()
		{
			Proxy.superclass.constructor.apply(this, arguments);
			var api = {};
			api[Ext.data.Api.actions.read] = true;
			this.api = api;
			
			this.url = String.format('{0}/Ajax/Compare/Photos', ASC.cfg.getLocaleContextPath());
		},
		
		/**
		 * Flatten the Photo data
		 * @method processResponse
		 * @private
		 */
		processResponse: function (response, params, reader, callback, scope, options)
		{
			
			//attempt to flatten the data
			var data = [];
			
			var strImageId = '{0}-{1}-{2}';
			
			function addRecord (acode, imgType, item)
			{	
				data.push({
					imageId: String.format(strImageId, acode, item.shotId, imgType),
					acode: acode,
					shotId: item.shotId,
					imageType: imgType,
					imageName: item.imgName
				});
			}
					
			try {
				Ext.each(response, function (record) {
					var acode = record.acode;
					Ext.each(record.exterior, addRecord.createDelegate(this, [acode, 'exterior'], 0), this);
					Ext.each(record.interior, addRecord.createDelegate(this, [acode, 'interior'], 0), this);
				}, this);
				
			} catch (e) {
				this.fireEvent("loadexception", this, params, response, e);
                callback.call(scope || this, null, options, false);
                return;
			}
			
			Proxy.superclass.processResponse.call(this, {rows: data}, params, reader, callback, scope, options);
		}
		
	});
	
	net.autodata.compare.data.photos.PhotosProxy = Proxy;
})();